<template>
    <div class="lyecs-goods-select-group">
        <el-space>
            <DialogForm v-if="gift_id == 0" isDrawer @okCallback="onOk" title="选择赠品" width="600px" path="promotion/productGift/src/SelectGift" :params="{ selected_ids: gifts, isMultiple: props.isMultiple }">
                <a>选择赠品</a>
            </DialogForm>
            <span v-if="isMultiple && gifts.length > 0" class="ml10">已选择 <b>{{ gifts.length }}</b> 个赠品</span>
            <el-button v-if="isMultiple && gifts.length > 0" @click="clear">清空</el-button>
        </el-space>
        <div class="lyecs-goods-selected-con" v-if="isMultiple && gifts.length > 0">
            <div class="goods-selected-list">
                <div class="goods-selected-list-tr goods-selected-list-th">
                    <div class="col col1">赠品名称</div>
                    <div class="col col2">商品信息</div>
                    <div class="col col2">赠品库存</div>
                    <div class="col col2">赠品数量</div>
                    <div class="col col3">操作</div>
                </div>
                <template v-for="(item, key) in gifts">
                    <div class="goods-selected-list-tr">
                        <div class="col col1">{{ item.gift_name }}</div>
                        <div class="col col2 goods-info">
                            <img width="50" height="50" :src="imageFormat(item.product_info?.pic_thumb)">
                            <span>{{ item.product_info?.product_name }}</span>
                        </div>
                        <div class="col col2">{{ item.gift_stock }}</div>
                        <div class="col col2 goods-info">
                            <el-input-number size="small" v-model="item.num" :min="1" :max="item.gift_stock" @change="changeNum(item)" />
                        </div>
                        <div class="col col3">
                            <a class="del-btn" @click="del(key)">删除</a>
                        </div>
                    </div>
                </template>
            </div>
        </div>
        <DialogForm v-if="!isMultiple && gift_name !== ''" isDrawer @okCallback="onOk" title="选择赠品" width="600px" path="promotion/productGift/src/SelectGift" :params="{ selected_ids: gifts, isMultiple: props.isMultiple }">
            <div class="flex flex-align-center cursor-pointer">
                <!-- <div class="pic">
                    <img :src="imageFormat(product_info?.pic_thumb)" style="width: 40px;">
                </div> -->
                <div class="gift_info">
                    <div class="gift_name">{{ gift_name }}</div>
                    <div class="gift_stock">库存: {{ gift_stock }}</div>
                </div>
            </div>
        </DialogForm>
    </div>
</template>
<script setup lang="ts">
import { ref, toRefs, onMounted, reactive } from "vue"
import { DialogForm } from '@/components/dialog'
import { message } from 'ant-design-vue'
import { ProductGiftFilterState } from '@/types/promotion/productGift.d';
import { imageFormat } from "@/utils/format";
interface GiftsForm {
    gift_name?: string,
    gift_id?: number,
    num?: number
}
// 传值
const props = defineProps({
    // 单选还是多选
    isMultiple: {
        type: Boolean,
        default: true
    }
})
const gifts = defineModel<ProductGiftFilterState[]>("gifts", { type: Array, default: [] });
const gift_id = defineModel<number>("gift_id", { type: Number, default: 0 });
const gift_stock = defineModel<number>("gift_stock", { type: Number, default: 0 });
const gift_name = defineModel<string>("gift_name", { type: String, default: '' });
const product_info = ref<any>({})
const emit = defineEmits(['update:gifts'])
const ids = ref<any>([])
onMounted(() => {
    // emit('update:gifts', gifts.value)
});
// 商品列表
const total = ref(0);
const onOk = (e:any) => {
    if(props.isMultiple){
        gifts.value = []
        e.map((item:any)=> {
            item.num = 1;
            gifts.value.push(item)
        })
        total.value = gifts.value.length
    }else{
        gifts.value = e
        gift_id.value = e[0].gift_id
        gift_name.value = e[0].gift_name
        gift_stock.value = e[0].gift_stock
        product_info.value = e[0].product_info
    }
}
// 清空
const clear = () => {
    gifts.value = [];
    ids.value = [];
}
// 删除
const del = (key: any) => {
    gifts.value.splice(<any>key, 1)
    ids.value.splice(<any>key, 1)
}
// 修改数量
const changeNum = (item:any) => {
    gifts.value.map(info => {
        if (info.gift_id === item.gift_id) {
            info.num = item.num
        }
    })
}

</script>

<style lang="less" scoped>
.all_brand {
    padding: 8px;
}

.all_brand ul {
    display: flex;
    flex-wrap: wrap;
    width: 290px;
}

.all_brand li a {
    padding: 2px 7px;
    display: inline-block;
    border-radius: 3px;
    line-height: 20px;
}

.all_brand li a:hover {
    background: #f4f4f4;
}

/*商品选择器*/
.lyecs-goods-select-group {
    margin-bottom: 0;
    width: 100%;
}

.lyecs-goods-select-group .lyecs-goods-selected-con {
    max-width: 710px;
    position: relative;
    padding-top: 50px;
    margin-top: 10px;
}

.lyecs-goods-select-group .clear-btn {
    margin-left: 20px;
}

.lyecs-goods-select-group .desc {
    margin-left: 20px;
    color: #999;
}

.lyecs-goods-select-group .goods-selected-list {
    margin-bottom: 20px;
    max-height: 550px;
    overflow-y: auto;
    min-width: 400px
}

.lyecs-goods-select-group .goods-selected-list-tr {
    display: flex;
    flex-wrap: nowrap;
    align-items: center;
    border-bottom: 1px solid #f0f0f0;
}

.lyecs-goods-select-group .goods-selected-list-th {
    color: #333;
    font-weight: bold;
    position: absolute;
    top: 0;
    width: 100%;
    background: #fff;
    height: 50px;
}

.lyecs-goods-select-group .goods-selected-list-tr .col {
    padding: 10px;
}


.lyecs-goods-select-group .goods-selected-list-tr .col1 {
    width: 100px;
}

.lyecs-goods-select-group .goods-selected-list-tr .col2 {
    flex: 1;
}

.lyecs-goods-select-group .goods-selected-list-tr .col3 {
    width: 80px;
}

.lyecs-goods-select-group .goods-selected-list-tr .goods-info {
    display: flex;
    flex-wrap: nowrap;
    align-items: center;
}

.lyecs-goods-select-group .goods-selected-list-tr .goods-info img {
    margin-right: 10px;
}
.gift_info{
    margin-left: 5px;
    font-size: 12px;
    color: #999;
}
</style>
